package com.jn.algorithm.day024.question75;

import java.util.Arrays;

/**
 * @author 江南大学1033190417
 * @date 2022/5/15 9:04
 */
public class Solution {
    public void sortColors(int[] nums) {
        int n = nums.length;
        int p0 = 0, p2 = n - 1;
        for (int i = 0; i < n; i++) {
            while (i < p2 && nums[i] == 2) {
                int temp = nums[p2];
                nums[p2] = 2;
                nums[i] = temp;
                --p2;
            }
            if (nums[i] == 0) {
                int temp = nums[p0];
                nums[p0] = 0;
                nums[i] = temp;
                p0++;
            }
        }
    }

    //单指针
    public void sortColors1(int[] nums) {
        int n = nums.length;
        int p0 = 0;
        for (int i = 0; i < n; i++) {
            if (nums[i] == 0) {
                int temp = nums[p0];
                nums[p0] = 0;
                nums[i] = temp;
                p0++;
            }
        }

        for (int i = 0; i < n; i++) {
            if (nums[i] == 1) {
                int temp = nums[p0];
                nums[p0] = 1;
                nums[i] = temp;
                p0++;
            }
        }
    }


    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] nums = new int[]{2, 0, 2, 1, 1, 0};
        solution.sortColors(nums);
        System.out.println(Arrays.toString(nums));
    }
}
